<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <!-- 引入 layui CSS -->
    <link rel="stylesheet" href="/static/layui.css">
    <!-- 引入自定义样式 -->
    <link rel="stylesheet" href="/static/login.css">
</head>
<body>
    <div class="login-container">
        <div class="login-title">用户登录</div>
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-block">
                    <input type="text" name="username" required lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-block">
                    <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button id="login-btn" class="layui-btn layui-btn-fluid" lay-submit lay-filter="login">登录</button>
                </div>
            </div>
            <div id="error-message" class="error-message layui-text"></div>
        </form>
    </div>
    
    <!-- 引入 layui JS -->
    <script src="/static/layui.js"></script>
    <script>
        // 初始化 layui
        layui.use(['form', 'layer'], function() {
            var form = layui.form;
            var layer = layui.layer;
            
            // 监听表单提交
            form.on('submit(login)', function(data) {
                // 禁用登录按钮防止重复提交
                var loginBtn = document.getElementById('login-btn');
                var originalText = loginBtn.textContent;
                loginBtn.textContent = '登录中...';
                loginBtn.disabled = true;
                
                // 使用URLSearchParams来构建表单数据，确保与FastAPI的OAuth2PasswordRequestForm兼容
                var formData = new URLSearchParams();
                formData.append('username', data.field.username);
                formData.append('password', data.field.password);
                
                // 发送登录请求到FastAPI的/token端点
                fetch('/token', {
                    method: 'POST',
                    body: formData
                })
                .then(response => {
                    if (!response.ok) {
                        throw new Error('登录失败');
                    }
                    return response.json();
                })
                .then(data => {
                    // 登录成功，保存token并跳转到欢迎页面
                    localStorage.setItem('access_token', data.access_token);
                    localStorage.setItem('token_type', data.token_type);
                    
                    // 获取用户信息并保存
                    fetch('/users/me', {
                        method: 'GET',
                        headers: {
                            'Authorization': data.token_type + ' ' + data.access_token
                        }
                    })
                    .then(response => {
                        if (response.ok) {
                            return response.json();
                        }
                        throw new Error('获取用户信息失败');
                    })
                    .then(userInfo => {
                        localStorage.setItem('userInfo', JSON.stringify(userInfo));
                        window.location.href = '/welcome';
                    })
                    .catch(() => {
                        // 获取用户信息失败，仍然跳转到欢迎页面
                        window.location.href = '/welcome';
                    });
                })
                .catch(error => {
                    // 登录失败，显示错误信息
                    document.getElementById('error-message').textContent = '登录失败：用户名或密码错误';
                    
                    // 恢复登录按钮
                    loginBtn.textContent = originalText;
                    loginBtn.disabled = false;
                });
                
                return false; // 阻止表单默认提交
            });
        });
    </script>
</body>
</html>